/// <reference path="../../sage/sage.util.js" />

var AdvancedSpritePainter = Base.extend({
    initialize: function initialize() {
        this.SHADOW_COLOR = "rgba(0, 0, 0, 0.3)";
    },
    paint: function paint(context) {
        var location = context.gameObject.locationRelativeToCamera,
            rect = location.objectRectangle,
            shRect = location.shadowRectangle,
            vp = context.viewPort,
            go = context.gameObject;

        if (location.visible) {

            if (rect.x > vp.size.w || rect.y > vp.size.h ||
                rect.getRight() < 0 || rect.getBottom() < 0 ||
                rect.w <= 0 || rect.h <= 0) return;

            //if (go.sprite == null)
            //go.sprite = AdvancedSprites.Spencer;
            var ss = go.sprite;

            if (ss == null) {
                var i = 23;
                i++;
            }
            else {
                var spriteState = "";
                if (go.speed <= 0) spriteState = "still";
                else if (go.speed >= 2 && ss.supportsState("run")) spriteState = "run";
                else spriteState = "walk";

                var direction = null;
                if (ss.directions == 8) {
                    direction = util.determine8WayDirection(location.placement.bearing);
                }
                else {
                    direction = util.determine4WayDirection(location.placement.bearing);
                }

                //TODO: need a sprite state! (Above)
                if (ss != null && ss.isReady()) {
                    go.frameIncrement = 1;
                    var fIdx = go.frameIndex, fCount = ss.getFrameCount(spriteState, direction);

                    if (fCount > 1) {
                        if (go.bounce) {
                            // Bounce Logic:
                            if (fIdx >= (fCount - 1))
                                go.frameIncrement = -1;
                            else if (fIdx <= 0)
                                go.frameIncrement = 1;
                            fIdx += go.frameIncrement;
                        }
                        else {
                            // Rotate Logic:
                            if (fIdx >= (fCount - 1))
                                fIdx = 0;
                            else
                                fIdx += go.frameIncrement;
                        }
                    }
                    else fIdx = 0;
                    go.frameIndex = fIdx;


                    var image = ss.getImage(spriteState, direction, fIdx);
                    var x = rect.x, y = rect.y, w = rect.w, h = rect.h, ih = ss.getHeight(spriteState, direction, fIdx), iw = ss.getWidth(spriteState, direction, fIdx);
                    w = (h * iw) / ih; // make the width 

                    if (!(shRect.x > vp.size.w || shRect.y > vp.size.h ||
                        shRect.getRight() < 0 || shRect.getBottom() < 0)) {
                        context.backLayer.fillStyle = this.SHADOW_COLOR;
                        util.fillEllipse(context.backLayer, shRect.x, shRect.y, w, shRect.h);
                    }

                    /*pctx.frontLayer.fillStyle = "#F00";
                    pctx.frontLayer.fillText(direction.toUpperCase() + " " + util.degrees(location.placement.bearing), rect.x, rect.y);*/


                    context.frontLayer.drawImage(image, x, y, w, h);
                }
            }
        }
    }
});
